package com.elive.controller.backend;

import com.elive.common.Const;
import com.elive.common.ServerResponse;
import com.elive.pojo.Apartment;
import com.elive.pojo.ApartmentDevice;
import com.elive.pojo.ApartmentService;
import com.elive.pojo.User;
import com.elive.service.IApartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by wuyin on 2018/3/13.
 */
@Controller
@RequestMapping("/apartment")
public class ApartmentManagerController {

    @Autowired
    private IApartmentService iApartmentService;

    @RequestMapping(value="getApartmentList.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getApartmentList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }

        return iApartmentService.getApartmentListByHostId(pageNum, pageSize, user.getId());
    }

    @RequestMapping(value="addApartment.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addApartment(Apartment apartment, String[] deviceIdList, String[] serviceIdList, HttpSession session){


        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        apartment.setHostId(user.getId());


        return iApartmentService.addApartment(apartment,deviceIdList,serviceIdList);
    }

    @RequestMapping(value="getApartmentInfo.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getApartmentInfo(Integer apartmentId, HttpSession httpSession){
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }else {

            return iApartmentService.getApartmentInfo(apartmentId);
        }
    }

    @RequestMapping(value ="editApartmentInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse editApartmentInfo(Apartment apartment, String[] deviceIdList, String[] serviceIdList, HttpSession httpSession){
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }else {
            apartment.setHostId(currentUser.getId());
            return iApartmentService.editApartmentInfo(apartment,deviceIdList,serviceIdList);
        }
    }

    @RequestMapping(value = "deleteApartment.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteApartment(Integer apartmentId, HttpSession httpSession){
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }else {
            return iApartmentService.deleteApartment(apartmentId);
        }
    }
}
